import Vue from 'vue'
import './plugins/fontawesome'
import './plugins/axios'
import App from './App.vue'
import router from './router'
import store from './store'
import './plugins/element.js'
import 'font-awesome/css/font-awesome.css'

import { postRequest } from './utils/api'
import { getRequest } from './utils/api'
import { putRequest } from './utils/api'
import { deleteRequest } from './utils/api'
import { initMenu } from './utils/menus'
import { downloadRequest } from './utils/download'

Vue.prototype.postRequest = postRequest;
Vue.prototype.getRequest = getRequest;
Vue.prototype.putRequest = putRequest;
Vue.prototype.deleteRequest = deleteRequest;
Vue.prototype.downloadRequset = downloadRequest;
Vue.config.productionTip = false

router.beforeEach((to, from, next) => {
  // to想要访问的路径
  // from代表从哪个路径跳转
  // next 是一个函数，表示放行
  if (window.sessionStorage.getItem("tokenStr")) {
    console.log("+++++++++++++++++++")
    initMenu(router, store)
    if (!window.sessionStorage.getItem('user')) {
      return getRequest('/admin/info').then(resp => {
        if (resp) {
          window.sessionStorage.setItem('user', JSON.stringify(resp))
          next()
        }
      })
    }
    return next()
  } else {
    if (to.path === '/login' || to.path === '/') {
      return next()
    }
    else {
      return next('/?redirect='+to.path)
    }
  }
})


new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
